{extend name="../../base/view/layout" /}
{block name="my_body"}
<div class="widgetbox box-inverse">
    <div class="widgetcontent wc1">

        <form id="form1" class="stdform" enctype="multipart/form-data" method="post" action="<?php echo url('shop/Goods/edit');?>">

            <div class="tabbable">
                <ul class="nav nav-tabs buttons-icons">
                    <li class="active"><a data-toggle="tab" href="#shop_info">基本信息</a></li>
                    <li><a data-toggle="tab" href="#shop_attr">商品属性</a></li>
                    <li><a data-toggle="tab" href="#shop_photos">商品相册</a></li>
                    <li><a data-toggle="tab" href="#shop_content">商品描述</a></li>
                    <li><a data-toggle="tab" href="#shop_desc">其他描述</a></li>
                </ul>
                <div class="tab-content">
                    <div id="shop_info" class="tab-pane active">

                        <div class="par control-group">
                            <label class="control-label" for="shop_id">店铺</label>
                            <span class="field">
                            <select name="shop_id" class="uniformselect" id="shop_id">
                                <?php foreach($shopData as $v):?>
                            	<option value="<?php echo $v['id'];?>" <?php if($v['id'] == $data['shop_id']) echo 'selected';?> ><?php echo $v['title'];?></option>
                                <?php endforeach;?>
                            </select>
                            </span>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="goods_name">商品名称</label>
                            <div class="controls"><input type="text" name="goods_name" id="goods_name" class="input-xlarge" value="<?php echo $data['goods_name'];?>" /></div>
                        </div>
                        <input type="hidden" name="id" value="<?php echo $data['id'];?>" />

                        <div class="par control-group">
                            <label class="control-label" for="goods_sn">商品货号</label>
                            <div class="controls"><input type="text" name="goods_sn" id="goods_sn" class="input-xlarge" value="<?php echo $data['goods_name'];?>" /></div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >配送属性</label>
                            <div class="controls">
                                <input type="radio" name="ps_attr" value="1"  <?php if($data['ps_attr'] == 1) echo "checked";?>  />发货
                                <input type="radio" name="ps_attr" value="2" <?php if($data['ps_attr'] == 2) echo "checked";?>  />不发货
                            </div>
                        </div>

                        <div class="par control-group ps_attr">
                            <label class="control-label" for="sheng_id">商品位置</label>
                            <span class="field">
                            <select name="sheng_id" class="uniformselect" id="sheng_id">
                            	<option value="">--请选择省份--</option>
                                <?php foreach($cityData as $k=>$v):?>
                                 <option value="<?php echo $v['id'];?>" <?php if($data['sheng_id'] == $v['id']) echo "selected";?> ><?php echo $v['name'];?></option>
                                <?php endforeach;?>
                            </select>
                            </span>
                        </div>
                        <div class="par control-group ps_attr">
                            <label class="control-label" for="shi_id"></label>
                            <span class="field">
                            <select name="shi_id" class="uniformselect" id="shi_id">
                            	<option value="<?php echo $data['shi_id'];?>"><?php echo $data['shi_name'];?></option>
                            </select>
                            </span>
                        </div>

                        <div class="par control-group ps_attr">
                            <label class="control-label" for="xian_id"></label>
                            <span class="field">
                            <select name="xian_id" class="uniformselect" id="xian_id">
                            	<option value="<?php echo $data['xian_id'];?>"><?php echo $data['xian_name'];?></option>
                            </select>
                            </span>
                        </div>

                        <div class="par control-group ps_attr">
                            <label class="control-label" for="coordinate">商品坐标位置</label>
                            <div class="controls"><input type="text" name="coordinate" id="coordinate" class="input-xlarge" value="<?php echo $data['coordinate'];?>" />&ensp;<a href="http://api.map.baidu.com/lbsapi/getpoint/index.html" target="_blank">选择坐标</a></div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="psfs_id">配送方式</label>
                            <span class="field">
                            <select name="psfs_id" class="uniformselect" id="psfs_id">
                            	<option value="0">包邮</option>
                            </select>
                            </span>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="brand_id">品牌</label>
                            <span class="field">
                            <select name="brand_id" class="uniformselect" id="brand_id">
                            	<option value="">--请选择--</option>
                                <?php foreach($brandData as $v):?>
                                <option value="<?php echo $v['id'];?>" <?php if($v['id'] == $data['brand_id']) echo 'selected';?> ><?php echo $v['name'];?></option>
                                <?php endforeach;?>
                            </select>
                            </span>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="cat_id">分类</label>
                            <span class="field">
                            <select name="cat_id" class="uniformselect" id="cat_id">
                            	<option value="">--请选择--</option>
                                <?php foreach($catData as $v):?>
                                <option value="<?php echo $v['id'];?>" <?php if($v['id'] == $data['cat_id']) echo 'selected';?>><?php echo str_repeat('-',$v['level']),$v['name'];?></option>
                                <?php endforeach;?>
                            </select>
                            </span>
                        </div>


                        <div class="par control-group">
                            <label class="control-label" for="promote_start_date">促销开始</label>
                            <div class="controls"><input type="text" name="promote_start_date" id="promote_start_date" class="input-xlarge" value="<?php echo date('Y-m-d H:i:s',$data['promote_start_date']);?>" /></div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="promote_end_date">促销结束</label>
                            <div class="controls"><input type="text" name="promote_end_date" id="promote_end_date" class="input-xlarge" value="<?php echo date('Y-m-d H:i:s',$data['promote_end_date']);?>" /></div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否新品</label>
                            <div class="controls">
                                <input type="radio" name="is_new" value="1"  <?php if($data['is_new'] == 1) echo "checked";?>  />是
                                <input type="radio" name="is_new" value="0" <?php if($data['is_new'] == 0) echo "checked";?>  />否
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否热卖</label>
                            <div class="controls">
                                <input type="radio" name="is_hot" value="1" <?php if($data['is_hot'] == 1) echo "checked";?>   />是
                                <input type="radio" name="is_hot" value="0" <?php if($data['is_hot'] == 0) echo "checked";?>  />否
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否精品</label>
                            <div class="controls">
                                <input type="radio" name="is_best" value="1" <?php if($data['is_best'] == 1) echo "checked";?>   />是
                                <input type="radio" name="is_best" value="0" <?php if($data['is_best'] == 0) echo "checked";?>  />否
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否推荐</label>
                            <div class="controls">
                                <input type="radio" name="is_floor" value="1" <?php if($data['is_floor'] == 1) echo "checked";?>   />是
                                <input type="radio" name="is_floor" value="0" <?php if($data['is_floor'] == 0) echo "checked";?>  />否
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否特价</label>
                            <div class="controls">
                                <input type="radio" name="is_discount" value="1" <?php if($data['is_discount'] == 1) echo "checked";?>   />是
                                <input type="radio" name="is_discount" value="0" <?php if($data['is_discount'] == 0) echo "checked";?>  />否
                                <span class="font-red">&ensp;&ensp;提示:特价商品没有会员价</span>
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" >是否自动上架</label>
                            <div class="controls">
                                <input type="radio" name="is_on_sale" value="1" <?php if($data['is_on_sale'] == 1) echo "checked";?>  />是
                                <input type="radio" name="is_on_sale" value="0" <?php if($data['is_on_sale'] == 0) echo "checked";?>   />否
                                <span class="font-red">&ensp;&ensp;提示:审核完成自动上架</span>
                            </div>
                        </div>

                        <div class="par control-group">
                            <label class="control-label" for="sort_num">排序</label>
                            <div class="controls"><input type="text" name="sort_num" id="sort_num" class="input-xlarge" value="100" value="<?php echo $data['sort_num'];?>" /></div>
                        </div>

                        <div class="control-group par">
                            <label class="control-label" for="logo">商品主图</label>
                            <div class="controls"><input type="file" name="logo" id="logo" class="input-xlarge"  /></div>
                        </div>
                        <div class="control-group par">
                            <label class="control-label" ></label>
                            <div class="controls"><?php showImage($data['sm_logo']);?></div>
                        </div>

                    </div><!--tab-pane-->


                    <!--属性-->
                    <div id="shop_attr" class="tab-pane">
                        <div class="par control-group">
                            <label class="control-label" >商品属性</label>
                            <span class="field">
                            <select name="type_id" class="uniformselect" id="type_id">
                                <?php foreach($typeData as $v):?>
                                <option value="<?php echo $v['id'];?>" <?php if($v['id'] == $data['type_id']) echo 'selected';?> ><?php echo $v['name'];?></option>
                                <?php endforeach;?>
                            </select>

                            <div>
                                <!--<ul id='attr_list'>-->
                                    <!--<li class="li-deltype">默认:<select name="attr_value[1][]"><option value="默认">默认</option></select></li>-->
                                <!--</ul>-->
                             <ul id='attr_list'>
                        <?php
                        $aids=array();
                        foreach($gaData as $k=>$v):
                                if(!in_array($v['attr_id'],$aids))
                                {
                                    $opt="[+]";
                                    $aids[]=$v['attr_id'];
                                    }
                                else{
                                    $opt="[-]";
                                    }
                            ?>
                        <li class="li-deltype">
                            <input type="hidden" name="goods_attr_id[]" value="<?php echo $v['id']; ?>" />
                            <?php if($v['attr_type']=='可选'):?>
                            <!--<a onclick="addNewAttr(this);" href="#">[+]</a>-->
                                <input style="clolr:red;background-color:#fff;border:0px;" type='button' onclick="addAttrBtn(this);" value='<?php echo $opt;?>' attr_id="<?php echo $v['id']; ?>" />
                            <?php endif;?>
                            <?php echo $v['attr_name'].':';?>
                            <?php if($v['attr_option_values']):
                                $attr_values=explode(',',$v['attr_option_values']);?>
                            <select name="attr_value[<?php echo $v['attr_id']; ?>][]">
                                    <option value=''>--请选择--</option>
                                <?php foreach($attr_values as $k1=>$v1):
                                        if($v1==$v['attr_value'])
                                            $select="selected='selected'";
                                        else
                                            $select="";
                                    ?>
                                    <option value='<?php echo $v1;?>' <?php echo $select;?>><?php echo $v1;?></option>
                                <?php endforeach;?>
                                </select>
                            <?php else:?>
                            <input type='text' name="attr_value[<?php echo $v['attr_id']; ?>][]" value="<?php echo $v['attr_value'];?>"
                            <?php endif;?><!--
-->                        </li>
                            <?php endforeach;?><!--
-->                   </ul>

                            </div>
                            </span>
                        </div>


                    </div>

                    <!--相册-->
                    <div id="shop_photos" class="tab-pane">

                        <div class="control-group par">
                            <!--<label class="control-label" for="logo">商品相册</label>-->
                            <div class="controls">
                                <ul>
                                    <li class="li-deltype"><input type="file" name="pic[]"  />&ensp;&ensp;&ensp;&ensp;<input type="button"  value='增加' class="btn btn-primary btn-rounded addpic" /></li>
                                </ul>
                            </div>

                            <div class="piclist" style="margin:20px;">
                                <ul>
                                    <?php foreach($gpData as $k=>$v):?>
                                    <li pid="<?php echo $v['id'];?>"><?php showImage($v['small_pic']);?><input type='button' value='x' class="delPic" /></li>
                                    <?php endforeach;?>
                                    <div class="clear"></div>
                                </ul>
                            </div>
                        </div>

                        <script>
                            var $ = jQuery.noConflict();
                        </script>
                        <!-- 配置文件 -->
                        <script type="text/javascript" src="/qiniu_ueditor_1.4.3/ueditor.config.js"></script>
                        <!-- 编辑器源码文件 -->
                        <script type="text/javascript" src="/qiniu_ueditor_1.4.3/ueditor.all.js"></script>

                    </div>

                    <div id="shop_content" class="tab-pane">

                        <textarea id="goods_desc" name="goods_desc" ><?php echo $good_desc['desc_content'];?></textarea>

                    </div>

                    <div id="shop_desc" class="tab-pane">

                        <div class="control-group par">
                            <!--<input type="button"  value='增加' desccount = '0' class="btn btn-primary btn-rounded adddesc" style="border:0px;" />-->
                            <div class="controls">
                                <ul class="desc_all">
                                    <?php foreach($other_desc as $k=>$v):?>
                                    <li class="li-deltype">
                                        <div class="par control-group">
                                            <!--<label class="control-label" >描述名称</label>-->
                                            <div class="controls" desc_id="<?php echo $v['id'];?>" desc_index="<?php echo $k;?>">描述名称:
                                                <input type="text" name="desc_name[]" value="<?php echo $v['desc_name'];?>"  class="input-xlarge" />
                                                <input type="button" onclick="deldesc(this)" value='删除' class="btn btn-primary btn-rounded " />
                                                <input type="button" onclick="editdesc(this)" value='保存修改' class="btn btn-primary btn-rounded " />
                                            </div>
                                        </div>
                                        <textarea id="desc_content<?php echo $k;?>" name="desc_content[]" ><?php echo $v['desc_content'];?></textarea>
                                        <script>
                                            UE.getEditor('desc_content<?php echo $k;?>', {
                                                initialFrameWidth : "100%",
                                                initialFrameHeight : "300"
                                            });
                                        </script>
                                    </li>
                                    <?php endforeach;?>
                                </ul>
                            </div>
                        </div>


                    </div>

                </div><!--tabcontent-->
            </div><!--tabbable-->


            <p class="stdformbutton">
                <input type="submit" class="btn btn-primary" />
            </p>
        </form>
    </div><!--widgetcontent-->
</div><!--widget-->
{/block}

{block name="my_js"}
<!--<script>-->
    <!--var $ = jQuery.noConflict();-->
<!--</script>-->
<!--&lt;!&ndash; 配置文件 &ndash;&gt;-->
<!--<script type="text/javascript" src="/qiniu_ueditor_1.4.3/ueditor.config.js"></script>-->
<!--&lt;!&ndash; 编辑器源码文件 &ndash;&gt;-->
<!--<script type="text/javascript" src="/qiniu_ueditor_1.4.3/ueditor.all.js"></script>-->
<script>
    UE.getEditor('goods_desc', {
        initialFrameWidth : "100%",
        initialFrameHeight : "500"
    });



    //删除描述
    function deldesc(a)
    {
        var desc_id = Number($(a).parent().attr('desc_id'));
        //请求后台删除属性
        var url = "<?php echo url('/base/Common/ajaxDelShopDesc');?>";
        if(confirm('确定要删除吗？')){
            $.ajax({
                url:url+"?id="+desc_id,
                type:'get',
                dataType:'json',
                success:function(data){
                    console.log(data.code);
                    if(data.code == 0)
                    {
                        $(a).parent().parent().parent().remove();
                    }
                    else
                    {
                        var html = '<div class="widgetbox box-danger"><h4 class="widgettitle">错误提示 <a class="close">×</a> <a class="minimize">–</a></h4><div class="widgetcontent">'+data.msg+'</div></div>';
                        var h = $(html);
                        $(".widgetbox").prepend(h);
                        window.setTimeout(function(){
                            h.remove();
                        },1000);
                    }
                }
            });
        }

    }

    //修改描述
    function editdesc(a)
    {
        var desc_id = Number($(a).parent().attr('desc_id'));
        var desc_index = Number($(a).parent().attr('desc_index'));
        var content = $('#desc_content'+desc_index).val();
        var desc_name = $(a).siblings("input[type='text']").val();
        //请求后台删除属性
        var url = "<?php echo url('/base/Common/ajaxEditShopDesc');?>";
        if(confirm('确定要修改吗？')){
            $.ajax({
                url:url,
                type:'POST',
                dataType:'json',
                data:'id='+desc_id+'&goods_desc='+content+'&desc_name='+desc_name,
                success:function(data){
                    if(data.code == 0)
                    {
                        var html = '<div class="widgetbox box-danger"><h4 class="widgettitle">保存成功 <a class="close">×</a> <a class="minimize">–</a></h4><div class="widgetcontent">'+data.msg+'</div></div>';
                        var h = $(html);
                        $(".widgetbox").prepend(h);
                        window.setTimeout(function(){
                            h.remove();
                        },1000);
                    }
                    else
                    {
                        var html = '<div class="widgetbox box-danger"><h4 class="widgettitle">错误提示 <a class="close">×</a> <a class="minimize">–</a></h4><div class="widgetcontent">'+data.msg+'</div></div>';
                        var h = $(html);
                        $(".widgetbox").prepend(h);
                        window.setTimeout(function(){
                            h.remove();
                        },1000);
                    }
                }
            });
        }

    }
</script>
<link href="/datetimepicker/jquery-ui-1.9.2.custom.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="/datetimepicker/jquery-ui-1.9.2.custom.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/datetimepicker/datepicker-zh_cn.js"></script>
<link rel="stylesheet" media="all" type="text/css" href="/datetimepicker/time/jquery-ui-timepicker-addon.min.css" />
<script type="text/javascript" src="/datetimepicker/time/jquery-ui-timepicker-addon.min.js"></script>
<script type="text/javascript" src="/datetimepicker/time/i18n/jquery-ui-timepicker-addon-i18n.min.js"></script>
<script>

    // 添加时间插件
    $.timepicker.setDefaults($.timepicker.regional['zh-CN']);  // 设置使用中文

    $("#promote_start_date").datetimepicker();
    $("#promote_end_date").datetimepicker();


    //<!--AJAX请求商品属性-->
    $("select[name='type_id']").change(function(){
        var type_id=$(this).val();
        var url = "<?php echo url('/base/Common/ajaxGetTypeAttr');?>";
        if(type_id>0){
            $.ajax({
                type:'GET',
                url:url+"?type_id="+type_id,
                dataType:'json',
                success:function(data){
                    var li='';
                    $('#attr_list').find('li').remove();
                    $(data).each(function(k,v){
                        li +="<li class='li-deltype'>";
                        if(v.attr_type=='可选'){
                            li +="<a href='#' onclick='addAttrBtn_a(this);'>[+]</a>";
                            li +=v.attr_name+":";
                            li +="<select name='attr_value["+v.id+"][]'>";
                            li +="<option value='请选择'>请选择</option>";
                            var attr_values=v.attr_option_values.split(',');
                            for(var i=0; i<attr_values.length;i++){
                                li +="<option value='"+attr_values[i]+"'>"+attr_values[i]+"</option>";
                            }
                            li +="</select>";
                        }else{
                            li +=v.attr_name+":";
                            li +="<input name='attr_value["+v.id+"][]' value='"+v.attr_option_values+"' />";
                        }
                    })
                    $('#attr_list').append(li);
                }
            })
        }
    });
    //增加属性
    function addAttrBtn(a){
        //先获取当前点击的按钮的value,判断是加好还是减号
        var li=$(a).parent();
        var attr_id=$(a).attr('attr_id');
        if($(a).attr('value')=='[+]'){
            var newLi=li.clone();
            newLi.find("option:selected").removeAttr("selected");
            // 把克隆出来的隐藏域里的ID清空
            newLi.find("input[name='goods_attr_id[]']").val("");
            newLi.find("input[type='button']").attr('value','[-]');
            li.after(newLi);
        }else{
            //请求后台删除属性
            var url = "<?php echo url('/base/Common/ajaxDelShopAttr');?>";
            var goods_id = "<?php echo $data['id'];?>";
            if(confirm('确定要删除吗？')){
                $.ajax({
                    url:url+"?attr_id="+attr_id+"&goods_id="+goods_id,
                    type:'get',
                    dataType:'json',
                    success:function(data){
                        console.log(data.code);
                        if(data.code == 0)
                        {
                            li.remove();
                        }
                        else
                        {
                            var html = '<div class="widgetbox box-danger"><h4 class="widgettitle">错误提示 <a class="close">×</a> <a class="minimize">–</a></h4><div class="widgetcontent">'+data.msg+'</div></div>';
                            var h = $(html);
                            $(".widgetbox").prepend(h);
                            window.setTimeout(function(){
                                h.remove();
                            },1000);
                        }
                    }
                });
            }
        }
    }

    //增加属性
    function addAttrBtn_a(a){
        //先获取当前点击的按钮的value,判断是加好还是减号
        var li=$(a).parent();
        if($(a).text()=='[+]'){
            var newLi=li.clone();
            newLi.find("a").text('[-]');
            li.after(newLi);
        }else{
            li.remove();
        }
    }
    //增加图片
    $('.addpic').click(function(){
        var li=$(this).parent();
        if($(this).attr('value')=='增加'){
            var newLi=li.clone(true);
            newLi.find("input[type='button']").attr('value','删除').css('background-color','#ffae00');
            li.after(newLi);
        }else{
            li.remove();
        }
    })

    //AJAX删除图片
    $('.delPic').click(function(){
        var li=$(this).parent();
        var pid=li.attr('pid');
        var url = "<?php echo url('/base/Common/ajaxDelPic');?>";
        if(confirm('确定要删除吗？')){
            $.ajax({
                url:url+"?pid="+pid,
                type:'get',
                success:function(data){
                    if(data.msg == 'ok')
                    {
                        li.remove();
                    }
                }
            });
        }
    })
</script>
<script>

    //地区三级联动
    //市
    $('#sheng_id').change(function(){
        var id=$(this).val();
        var url = "<?php echo url('base/City/getCityById');?>";
        $.ajax({
            url:url+'?id='+id,
            type:"GET",
            dataType:"json",
            success:function(data){
                var option="<option value=''>--请选择--</option>";
                $(data).each(function(k,v){
                    option +="<option value='"+v.id+"'>";
                    option +=v.name;
                    option +="</option>";
                })
                $('#shi_id').empty();
                $('#shi_id').append(option);
            }
        });
    });
    //区
    $('#shi_id').change(function(){
        var id=$(this).val();
        var url = "<?php echo url('base/City/getCityById');?>";
        $.ajax({
            url:url+'?id='+id,
            type:"GET",
            dataType:"json",
            success:function(data){
                var option="<option value=''>--请选择--</option>";
                $(data).each(function(k,v){
                    option +="<option value='"+v.id+"'>";
                    option +=v.name;
                    option +="</option>";
                })
                $('#xian_id').empty();
                $('#xian_id').append(option);
            }
        });
    });

    //商品位置
    <?php if($data['ps_attr'] == 1):?>
    $(".ps_attr").hide();
    <?php endif;?>

    $("input[name='ps_attr']").change(function(){
        var val = $(this).val();
        if(val == 2){
            $(".ps_attr").show();
        } else {
            $(".ps_attr").hide();
        }
    })
</script>
{/block}